package com.xiaobaibai.server.user;

import com.alibaba.fastjson.JSON;
import com.xiaobaibai.config.MyException;
import com.xiaobaibai.dao.UserAdminDao;
import com.xiaobaibai.dao.UserDao;
import com.xiaobaibai.pojo.User;
import com.xiaobaibai.pojo.UserAdmin;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements IUserService{

    @Autowired
    private UserAdminDao userAdminDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String login(String userId, String password) {
        password = DigestUtils.md5Hex(password)
                .replaceAll("1","2")
                .replaceAll("3","5")
                .replaceAll("7","8");
        UserAdmin userAdmin = userAdminDao.login(userId, password);

        Assert.notNull(userAdmin,"用户名或密码错误");

        String token = UUID.randomUUID().toString()+'@'+userId;

        stringRedisTemplate.opsForValue().set("userId:"+userId,token,
                3, TimeUnit.HOURS);

        return token;
    }

    @Override
    public String getPasswordByUserId(String userId) {
        return userAdminDao.getPasswordByUserId(userId);
    }

    @Override
    public UserAdmin getUserInfo(String userId){
        return userAdminDao.getUserInfoByUserId(userId);
    }

    @Override
    public List<User> getUserList() {

        //还有新用户无视最后上线时间然后排在前面,点击欢迎(已读)
        Set<String> newUserId = stringRedisTemplate.opsForSet().members("newUser");
        List<User> userList = userDao.getUserList();

        if(newUserId.size()>=1) {
            List<User> list = new ArrayList<>(userList.size());
            List<User> result = new ArrayList<>(userList.size());
            for (User user : userList) {
                //开始遍历整个userList

                if(newUserId.contains(user.getId().toString())){
                    //当前用户是新用户
                    user.set_isNew(true);
                    result.add(user);
                    newUserId.remove(user.getId().toString());
                }else{
                    //不是新用户
                    list.add(user);
                }

            }
            result.addAll(list);//新用户无序在前,老用户根据最后上线时间在后
            return result;
        }
        return userList;
    }

    @Override
    public void welcomeNewUser(String id) {
        stringRedisTemplate.opsForSet().remove("newUser",id);
    }

    @Override
    public void delUser(String id) {
        User user = userDao.selectByPrimaryKey(Integer.parseInt(id));
        userDao.deleteByPrimaryKey(Integer.parseInt(id));//里面两条
        userDao.deleteQQUser(user.getUserId());
        stringRedisTemplate.opsForValue().increment("UserCount",-1);
    }
}
